package com.hq.door.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hq.door.dao.TOutRequestDAO;
import com.hq.door.dao.TRecordsDAO;
import com.hq.door.dao.TStudentDAO;
import com.hq.door.dto.OutrequestInfo;
import com.hq.door.dto.StudentInfo;
import com.hq.door.dto.TRecordInfo;
import com.hq.door.pojo.TOutRequest;
import com.hq.door.pojo.TRecords;
import com.hq.door.pojo.TStudent;
import com.hq.door.pojo.TTeacher;
import com.hq.door.service.IStudentFrontService;

@Service("studentFrontService")
public class StudentFrontServiceImpl extends BaseServiceImpl<TStudent>
		implements IStudentFrontService {

	TStudentDAO tStudentDAO;
	@Autowired
	TRecordsDAO tRecordsDAO;
	@Autowired
	TOutRequestDAO tOutRequestDAO;

	public TStudentDAO gettStudentDAO() {
		return tStudentDAO;
	}

	@Autowired
	public void settStudentDAO(TStudentDAO tStudentDAO) {
		this.tStudentDAO = tStudentDAO;
		super.setBaseDao(tStudentDAO);
	}

	@Override
	public TStudent login(String code, String password) {
		String hql = "from TStudent ts inner join fetch ts.TClass where ts.code=? and ts.password=?";
		TStudent tStudent = tStudentDAO.queryOne(hql, code, password);

		return tStudent;
	}

	@Override
	public StudentInfo getCurStu(TStudent tStudent) {

		StudentInfo studentInfo = new StudentInfo();
		if (tStudent.getTClass() != null)
			studentInfo.setClassname(tStudent.getTClass().getName());
		studentInfo.setName(tStudent.getName());

		return studentInfo;

	}

	@Override
	public List<TRecordInfo> getCardRecords(TStudent tStudent, Date sDate,
			Date eDate, int pageSize, int pageStart, int type) {
		List<TRecordInfo> tRecordInfos = new ArrayList<TRecordInfo>();
		String hql = null;
		List<TRecords> tRecords = null;
		pageStart = (pageStart - 1) * pageSize;
		if (type == 0) {
			hql = "select tr from TRecords tr, TStudent ts where ts.cordNo=tr.cardNo and "
					+ "tr.inoutType=1 and tr.isException=0 and tr.createTime>? and tr.createTime<? and ts.cordNo=?";
		} else if (type == 1) {
			hql = "select tr from TRecords tr, TStudent ts where ts.cordNo=tr.cardNo and "
					+ "tr.inoutType=0 and tr.isException=0 and tr.createTime>? and tr.createTime<? and ts.cordNo=?";
		} else {
			hql = "select tr from TRecords tr, TStudent ts where ts.cordNo=tr.cardNo and "
					+ "tr.isException=1 and tr.createTime>? and tr.createTime<? and ts.cordNo=?";
		}
		hql = hql + "order by tr.createTime desc";
		tRecords = tRecordsDAO.findPageObjects(hql, pageStart, pageSize, sDate,
				eDate, tStudent.getCordNo());
		tRecordInfos = new TRecordInfo().getTOutrequest(tRecords);
		for (TRecordInfo tRecordInfo : tRecordInfos) {
			tRecordInfo.setStudentName(tStudent.getName());
		}
		return tRecordInfos;
	}

	@Override
	public int getCount(TStudent tStudent, Date sDate, Date eDate, int type) {
		String hql = null;
		if (type == 0) {
			hql = "select count(tr.id) from TRecords tr, TStudent ts where ts.cordNo=tr.cardNo and "
					+ "tr.inoutType=1 and tr.isException=0 and tr.createTime>? and tr.createTime<? and ts.cordNo=?";
		} else if (type == 1) {
			hql = "select count(tr.id) from TRecords tr, TStudent ts where ts.cordNo=tr.cardNo and "
					+ "tr.inoutType=0 and tr.isException=0 and tr.createTime>? and tr.createTime<? and ts.cordNo=?";
		} else {
			hql = "select count(tr.id) from TRecords tr, TStudent ts where ts.cordNo=tr.cardNo and "
					+ "tr.isException=1 and tr.createTime>? and tr.createTime<? and ts.cordNo=?";
		}
		hql = hql + "order by tr.createTime desc";
		int res = tRecordsDAO.getCount(hql, sDate, eDate, tStudent.getCordNo());
		return res;
	}

	@Override
	public List<OutrequestInfo> getOutRequest(TStudent tStudent,
			Integer pageSize, Integer pageStart) {
		List<OutrequestInfo> outrequestInfos = new ArrayList<OutrequestInfo>();
		String hql = null;
		List<TOutRequest> tOutRequest = null;
		pageStart = (pageStart - 1) * pageSize;
		hql = "select tor from TOutRequest tor inner join fetch tor.TStudent ts where ts.code=? order by tor.requestTime desc";
		tOutRequest = tOutRequestDAO.findPageObjects(hql, pageStart, pageSize,
				tStudent.getCode());
		outrequestInfos = new OutrequestInfo().getTOutrequest(tOutRequest);
		for (OutrequestInfo OutrequestInfo : outrequestInfos) {
			OutrequestInfo.setStudentname(tStudent.getName());
		}
		return outrequestInfos;
	}

	@Override
	public int getRequestCount(TStudent tStudent) {
		String hql = "select count(tor.id) from TOutRequest tor inner join tor.TStudent ts where ts.code=? order by tor.requestTime desc";
		int res = tRecordsDAO.getCount(hql, tStudent.getCode());
		return res;
	}

	@Override
	public int addOutRequest(TStudent tStudent, OutrequestInfo outrequestInfo) {
		TOutRequest tOutRequest = new TOutRequest();
		tStudent = tStudentDAO.get(tStudent.getId());
		if (tStudent == null || tStudent.getTClass() == null
				|| tStudent.getTClass().getTTeacher() == null) {
			return 0;
		}
		tOutRequest.setTStudent(tStudent);
		tOutRequest.setTTeacher(tStudent.getTClass().getTTeacher());
		tOutRequest.setAuthStatus(0);
		tOutRequest.setIsAgent(0);
		tOutRequest.setStartTime(outrequestInfo.getStartTime());
		tOutRequest.setEndTime(outrequestInfo.getEndTime());
		tOutRequest.setRequestTime(outrequestInfo.getRequestTime());
		tOutRequest.setRequestReason(outrequestInfo.getRequestReason());

		tOutRequestDAO.save(tOutRequest);
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public TStudent weChatLogin(String wexinId) {
		String hql = "from TStudent ts inner join fetch ts.TClass where ts.weixinId=?";
		TStudent tStudent = tStudentDAO.queryOne(hql, wexinId);

		return tStudent;
	}

	@Override
	public TStudent modifyStudent(String name, String password, String weixinId) {
		TStudent tStudent = null;
		StringBuffer hql = new StringBuffer(
				"from TStudent ts inner join fetch ts.TClass tc where ts.code=? and ts.password=?");
		tStudent = tStudentDAO.queryOne(hql.toString(), name, password);
		if (tStudent != null) {
			tStudent.setWeixinId(weixinId);
			tStudentDAO.update(tStudent);
		}
		return tStudent;
	}

}
